package com.hframe.type;

import com.hframe.Json.JsonFiletr;
import com.hframe.param.ParamFiletr;
import com.hframe.typefilter.FilterRef;
import com.hframe.typefilter.LongBase64Decoder;
import com.hframe.typefilter.LongBase64Encoder;
import com.hframe.xml.XmlFiletr;

/**
 * Created by heqian on 17-5-23.
 */
public class TId extends TLong {
    public TId() {
        this.className = Long.class;

        FilterRef setCall = new FilterRef() {
            @Override
            public Object filter(Object object) throws Exception {
                return decoderId(object);
            }
        };

        FilterRef getCall = new FilterRef() {
            @Override
            public Object filter(Object object) throws Exception {
                return encoderId((Long) object);
            }
        };

        TFilter filter = filter(JsonFiletr.class);
        filter.setGetCall(getCall);
        filter.setSetCall(setCall);

        filter = filter(ParamFiletr.class);
        filter.setGetCall(getCall);
        filter.setSetCall(setCall);

        filter = filter(XmlFiletr.class);
        filter.setGetCall(getCall);
        filter.setSetCall(setCall);
    }

    public TId(Long adminId) {
        this();
        this.object = adminId;
    }

    public static Long decoderId(Object object) {
        if (null != object && !"null".equals(object) && object instanceof String)
            return LongBase64Decoder.decoder(((String) object).replace("!", "/").replace("-", "="));
        return null;
    }

    public static String encoderId(Long object) {
        String ret = LongBase64Encoder.encoder(object);
        return null != ret ? ret.replace("/", "!").replace("=", "-") : null;
    }

    @Override
    public String out() {
        String ret = encoderId(object);
        return null != ret ? ret : "";
    }
}
